package com.btioe.yugong.common.conf;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

@Slf4j
public class ConfigLoader {
    static final String CONFIG_PROPERTIES_NAME_FORMAT = "conf_%s.properties";
    static Properties properties;

    private static Properties getConfig(String env) {
        if (properties == null) {
            properties = new Properties();
            String confPath = String.format(CONFIG_PROPERTIES_NAME_FORMAT, env);
            try {
                ClassPathResource classPathResource = new ClassPathResource(confPath);   //这里的填写的参数是配置文件的相对路径
                properties.load(new InputStreamReader(classPathResource.getInputStream(), "gbk"));     //文件流的编码方式
            } catch (IOException e) {
                log.error("", e);
            }
            return properties;
        } else {
            return properties;
        }
    }

    public static ProfileBO getProfile(String env) {
        try {
            return ProfileBO.buildFromProperties(getConfig(env));
        } catch (IllegalAccessException e) {
            log.error("", e);
        }
        return null;
    }
}
